package com.dating.libs.web.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public final class OAuthService {
	private final String EMAIL="EMAIL";
	private final String SECRETKEY="SECRET_KEY";
	private final String REFRESH_TOKEN="REFRESH_TOKEN";
	
	public java.lang.String hello(java.lang.String name) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace, "hello");
		_client.addProperty("name", name);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call(Configuration.wsNameSpace, _envelope, setHeader()); // set header
		SoapObject body = (SoapObject) _envelope.bodyIn;
		return String.valueOf(body.getProperty("return"));
		// return (java.lang.String) _envelope.bodyIn;
	}

	public Credential requestCredential(String username,String password) throws NumberFormatException,Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"requestCredential");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call(Configuration.wsNameSpace, _envelope, setHeader(username,password)); // set header
		SoapObject _ret = (SoapObject) _envelope.bodyIn;
		SoapObject _ret1=(SoapObject)_ret.getProperty("return");
		
		String expireTime=_ret1.getProperty("expireTime").toString();
		String refreshToken=_ret1.getProperty("refreshToken").toString();
		String token=_ret1.getProperty("token").toString();
		int userID=Integer.parseInt(_ret1.getProperty("userID").toString());
		
		Credential _returned = new Credential();
		
		/*
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy H:m:s");
		try{
			date = (Date) sdf.parse(expireTime);
			Calendar cl=Calendar.getInstance();
			cl.setTime(date);
			Log.e("exprise", expireTime);
			Log.e("date", cl.YEAR+"");
		}catch(ParseException e){
			e.printStackTrace();
		}
		return null;
		*/
		try{
		SimpleDateFormat smpDate=new SimpleDateFormat("MM/dd/yyyy H:m:s");
		Date date=(Date)smpDate.parse(expireTime); 
		java.util.Calendar calenda=Calendar.getInstance();
		calenda.setTime(date);
		_returned.setExpireTime(calenda);
		}catch(ParseException e){
			Log.e("calenda", "parse error");
			_returned.setExpireTime(null);
		}
		
		_returned.setToken(token);
		_returned.setRefreshToken(refreshToken);
		_returned.setUserID(userID);
		return _returned;
	}

	public Credential requestRefreshToken() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"requestRefreshToken");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call(Configuration.wsNameSpace, _envelope, setHeader());
		SoapObject _ret = (SoapObject) _envelope.bodyIn;
		int _len = _ret.getPropertyCount();
		Credential _returned = new Credential();
		for (int _i = 0; _i < _len; _i++) {
			_returned.setProperty(_i, _ret.getProperty(_i));
		}
		return _returned;
	}

	/************************************ CONFIG ***********************************/
	private void addMapping(SoapSerializationEnvelope envelope) {
		envelope.addMapping(Configuration.wsNameSpace, "credential",
				new Credential().getClass()); // IMPORTANT!!!
	}
	
	private List<HeaderProperty> setHeader() {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty("SessionID", "1"));
		headerList.add(new HeaderProperty("AuthenticateToken", "1"));

		return headerList;
	}
	private List<HeaderProperty> setHeader(String arg0,String arg1) {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty(EMAIL, arg0));
		headerList.add(new HeaderProperty(SECRETKEY, arg1));

		return headerList;
	}
	@SuppressWarnings("unused")
	private List<HeaderProperty> setRefreshHeader(String arg0,String arg1) {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty(EMAIL, arg0));
		headerList.add(new HeaderProperty(REFRESH_TOKEN, arg1));

		return headerList;
	}
}
